package com.suitsoft.bdms.web;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.bdms.commos.Constants;
import com.suitsoft.bdms.commos.LoginUser;
import com.suitsoft.bdms.commos.UserHolder;
import com.suitsoft.bdms.entity.account.Authority;
import com.suitsoft.bdms.entity.account.Role;

/**
 * 主Action
 * <pre>根据 登陆用户的权限。跳转到相应的界面</pre>
 *@author Suine
 */
@Results({
	@Result(name="GENERAL",location="point-login.action",type="redirect"),
	@Result(name="LGINOUT",location = "login.action",type="redirect")
}
)
public class MainAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5736279121732194379L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private final String LGINOUT = "LGINOUT";
	private final String GENERAL = "GENERAL";
	
	private LoginUser loginUser;

	@Override
	public String execute() throws Exception {
		loginUser = UserHolder.getLoginUser();
		if(loginUser == null){
			return LGINOUT;
		}
		boolean f = false;
		//遍历判断 用户类型
		for(Role r : loginUser.getRoleList()){
			for(Authority a : r.getAuthorityList()){
				if(Constants.cn_ADMIN_USER.equals(a.getName())){
					f = true;
					break;
				}
			}
			if(f){
				break;
			}
		}
		//ID为1 是管理员
		//不管有没有 资源都可以直接登陆用管理员界面
		if(loginUser.getId().equals(1L)){
			return SUCCESS;
		}
		if(f){
			return SUCCESS;
		}else{
			return GENERAL;
		}
	}
}

